#include "lab3.h"
using namespace std;
#include<iostream>
#include<string>

Student::~Student( void )
{
	delete [] m_FirstName;
	delete [] m_LastName;
	delete [] m_fgrades;
}

Student::Student( Student & student )
: m_FirstName( NULL ), m_LastName( NULL ), m_numGrades( 0 ), m_fgrades( NULL )
{
	*this = student;
}

Student::Student( char *pFN,  char *pLN, int numGrades, const float *pGrades)
{
	m_FirstName = new char[ strlen(pFN) + 1 ];
	strcpy( m_FirstName, pFN );
	m_LastName = new char[ strlen(pLN) + 1 ];
	strcpy( m_LastName,  pLN );
	m_numGrades = numGrades;
	m_fgrades = new float[m_numGrades];
	// TODO: Copy all grades to m_fgrades
	for ( int i = 0; i < numGrades; ++i )
		m_fgrades[i] = pGrades[i];
}


void Student::display( void )
{
	cout << "First Name: " << m_FirstName << "\t";
	cout << "Last Name: " << m_LastName << "\t";
	cout << "Average Grade: " << GetAverageGrade() << endl;
	cout << "-------------------------------------------------------" << endl;
}


Student & Student::operator = ( Student &rhs )
{
	// paranoid check
	if ( &rhs == this )
		return *this;

	if ( m_FirstName )
		delete [] m_FirstName;
	if ( m_LastName )
		delete [] m_LastName;
	if ( m_fgrades )
		delete [] m_fgrades;

	m_numGrades = rhs.m_numGrades;

	m_FirstName = new char[strlen(rhs.m_FirstName) + 1];
	m_LastName	= new char[strlen(rhs.m_LastName) + 1];
	m_fgrades	= new float[rhs.m_numGrades];

	strcpy( m_FirstName, rhs.m_FirstName );
	strcpy( m_LastName, rhs.m_LastName);

	for ( int i = 0; i < rhs.m_numGrades; ++i )
		m_fgrades[i] = rhs.m_fgrades[i];

	return *this;
}

float Student::GetAverageGrade( void )
{
	int average = 0;

	for ( int i = 0; i < m_numGrades; ++i )
	{
		average += m_fgrades[i];
	}

	return average / m_numGrades;
}
